//
//  oldGifView.m
//  laonianbang
//
//  Created by 勇拓 李 on 17/10/24.
//  Copyright © 2017年 zaiyou. All rights reserved.
//

#import "oldGifView.h"

#define  GIF_WIDTH 100*2

#import <ImageIO/ImageIO.h>
#import <MobileCoreServices/MobileCoreServices.h>
@interface oldGifView()
@property (nonatomic, strong)NSMutableArray<UIImage *> *images;
@property (nonatomic, strong)oldGifView *loading;
@property (nonatomic, strong)UIView *gifContentView;

@property (nonatomic, assign)CGImageSourceRef gif;
@property (nonatomic, strong)NSDictionary *gifDic;
@property (nonatomic, assign)size_t index;
@property (nonatomic, assign)size_t count;
@property (nonatomic, strong)NSTimer *timer;

@end


@implementation oldGifView

- (instancetype)init{
    self = [self initWithFrame:CGRectMake(0, 0, GIF_WIDTH, GIF_WIDTH)];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.layer.cornerRadius = GIF_WIDTH/2;
        self.layer.masksToBounds = YES;
        [self createGif];
        
    }
    return self;
}

- (void)createGif{
    
    //    _gifContentView.layer.borderColor = UIColorFromRGB(No_Choose_Color).CGColor;
    //    _gifContentView.layer.borderWidth = 1.0;
    NSDictionary *gifLoopCount = [NSDictionary dictionaryWithObject:[NSNumber numberWithInt:0] forKey:(NSString *)kCGImagePropertyGIFLoopCount];
    _gifDic = [NSDictionary dictionaryWithObject:gifLoopCount forKey:(NSString *)kCGImagePropertyGIFDictionary];
    
    NSData *gif = [NSData dataWithContentsOfFile: [[NSBundle mainBundle] pathForResource:@"雷达图" ofType:@"gif"]];
    _gif = CGImageSourceCreateWithData((CFDataRef)gif, (CFDictionaryRef)_gifDic);
    _count = CGImageSourceGetCount(_gif);
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(startLoading) userInfo:nil repeats:YES];
    [_timer fire];
}
-(void)startLoading
{
    _index ++;
    _index = _index%_count;
    CGImageRef ref = CGImageSourceCreateImageAtIndex(_gif, _index, (CFDictionaryRef)_gifDic);
    self.layer.contents = (__bridge id)ref;
    CFRelease(ref);
}
+ (void)startLoading:(UIView *)view{
    oldGifView *loading = [[oldGifView alloc]init];
//    UIWindow *keyView = [UIApplication sharedApplication].keyWindow;
    loading.frame = CGRectMake((SCREEN_WIDTH - 200)/2, (view.frame.size.height - 360)/2, 200, 200);
//    CGRect frame = loading.frame;
//    frame.origin.y -= 110;
//    loading.frame = frame;

    
    
    [view addSubview:loading];
    
//    loading.center = view.center;
    
//    dispatch_main_after(5.0f, ^{
//        [oldGifView endLoading];
//    });
}
+ (void)endLoading:(UIView *)keyView{
    for (UIView *view in keyView.subviews) {
        if ([view isKindOfClass:[oldGifView class]]) {
            //            [UIView animateWithDuration:1.0 animations:^{
            //                view.alpha = 0;
            //            } completion:^(BOOL finished) {
            [((oldGifView *)view) stopGif];
            [view removeFromSuperview];
            //            }];
        }
    }
    
}
- (void)stopGif
{
    [_timer invalidate];
    _timer = nil;
}
- (void)dealloc{
    CFRelease(_gif);
}

//static void dispatch_main_after(NSTimeInterval delay, void (^block)(void))
//{
//    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
//        block();
//    });
//}


@end
